package com.easy.dn.ui.main.model;

import com.easy.dn.bean.ListBean;
import com.easy.dn.bean.PinnedHeaderBean;
import com.easy.dn.bean.ProfessionTypeBean;
import com.easy.dn.ui.main.adapter.BaseHeaderAdapter;
import com.easy.dn.ui.main.contract.ProfessionTypeContract;
import com.easy.dn.utils.DnUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 丁宁
 * on 2017/5/16.
 */

public class ProfessionTypeModel implements ProfessionTypeContract.Model {

    private List<ProfessionTypeBean> mProfessionTypeBeans;
    private List<ProfessionTypeBean> mChildData = new ArrayList<ProfessionTypeBean>();
    private List<ProfessionTypeBean> mParentData = new ArrayList<ProfessionTypeBean>();

    @Override
    public Observable<ListBean> getProfessionList(final Map<String, ProfessionTypeBean> mSelect) {
        return Observable.create(new Observable.OnSubscribe<ListBean>() {
            @Override
            public void call(Subscriber<? super ListBean> subscriber) {
                mProfessionTypeBeans = DnUtils.getProfessionTypeList();
                List<PinnedHeaderBean<ProfessionTypeBean>> mData = new ArrayList<>();
                // 区分标题 和 可点击的内容
                for (ProfessionTypeBean entity : mProfessionTypeBeans) {
                    if (entity.getParentId() != 0) {
                        // 选中之后 设置选中状态
                        if (mSelect != null && mSelect.size() > 0) {
                            for (String selectName : mSelect.keySet()) {
                                if (entity.getTradeTypeName().equals(selectName)) {
                                    entity.setIsChecked(true);
                                }
                            }
                        }
                        mChildData.add(entity);
                        mData.add(new PinnedHeaderBean<ProfessionTypeBean>(entity, BaseHeaderAdapter.TYPE_DATA, ""));
                    } else {
                        mParentData.add(entity);
                        mData.add(new PinnedHeaderBean<ProfessionTypeBean>(null, BaseHeaderAdapter.TYPE_HEADER, entity.getTradeTypeName()));
                    }
                }

                ListBean bean = new ListBean();
                bean.setData(mData);
                subscriber.onNext(bean);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<ListBean> getSelectData(final Map<String, ProfessionTypeBean> mSelect, final String filterStr) {
        return Observable.create(new Observable.OnSubscribe<ListBean>() {
            @Override
            public void call(Subscriber<? super ListBean> subscriber) {
                if (mProfessionTypeBeans == null) {
                    mProfessionTypeBeans = DnUtils.getProfessionTypeList();
                }
                List<ProfessionTypeBean> filterProfessionTypeList = DnUtils.getFilterProfessionTypeList(mParentData, mChildData, filterStr);
                List<PinnedHeaderBean<ProfessionTypeBean>> mData = new ArrayList<>();
                // 区分标题 和 可点击的内容
                for (ProfessionTypeBean entity : filterProfessionTypeList) {
                    if (entity.getParentId() != 0) {
                        // 选中之后 设置选中状态
                        if (mSelect != null && mSelect.size() > 0) {
                            for (String selectName : mSelect.keySet()) {
                                if (entity.getTradeTypeName().equals(selectName)) {
                                    entity.setIsChecked(true);
                                }
                            }
                        }
                        mData.add(new PinnedHeaderBean<ProfessionTypeBean>(entity, BaseHeaderAdapter.TYPE_DATA, ""));
                    } else {
                        mData.add(new PinnedHeaderBean<ProfessionTypeBean>(null, BaseHeaderAdapter.TYPE_HEADER, entity.getTradeTypeName()));
                    }
                }

                ListBean bean = new ListBean();
                bean.setData(mData);
                subscriber.onNext(bean);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
